﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RegisterForm.cs" company="HelloWorld">
//   BY田新宇
// </copyright>
// <summary>
//  注册窗口
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Form
{
    using System;
    using System.Windows.Forms;

    using WindowsFormsApp2.Service;

    /// <summary>
    /// The register form.
    /// </summary>
    public partial class RegisterForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RegisterForm"/> class.
        /// </summary>
        public RegisterForm()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// The register_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void 注册_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false; // 关闭注册窗口的最大化功能
            this.button1.Enabled = false; // 用户未输入注册信息时，默认注册按钮不可点击

            this.Verification_Code.Text = VerificationCode.Code(); // 加载验证码
        }

        // 返回

        /// <summary>
        /// The button 2_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button2_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        // 注册

        /// <summary>
        /// The button 1_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button1_Click(object sender, EventArgs e)
        {
            var db = new LinkSqLiteDB();   // 创建数据库连接对象
            if (db.DBConn())
            { // 连接数据库
                var str = "insert into login_info(stuid,password,stuname) values ('" + this.textBox1.Text + "','" + this.textBox3.Text + "','" + this.textBox2.Text + "');";
                const string Info = "注册成功！";

                if (db.UpdateDeleteAdd(str))
                {
                    var dr = MessageBox.Show(Info, @"标题", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    if (dr == DialogResult.OK)
                    {
                        this.Visible = false; // 隐藏当前窗口
                    }
                }
            }

            db.DBClose(); // 关闭数据连接
        }

        // 当用户输入学号时验证该学号是否可以注册

        /// <summary>
        /// The text box 1_ leave.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void TextBox1_Leave(object sender, EventArgs e)
        {
            var db = new LinkSqLiteDB();   // 创建数据库连接对象
            if (db.DBConn())
            {          // 创建数据库连接
                var str = "select * from login_info where stuid ='" + this.textBox1.Text + "'";
                if (db.GetLoginData(str))
                {
                    // 执行sql检测该用户是否已经被注册
                    MessageBox.Show(@"该用户已经存在!");
                }
            }

            db.DBClose(); // 关闭数据库连接
        }

        /// <summary>
        /// The text box 4_ leave.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void TextBox4_Leave(object sender, EventArgs e)
        {
            if (!this.textBox3.Text.Equals(this.textBox4.Text))
            {
                MessageBox.Show(@"密码不一致！");
            }
        }

        /// <summary>
        /// The text box 5_ key up.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void TextBox5_KeyUp(object sender, KeyEventArgs e)
        {
            if (!this.textBox5.Text.Equals(this.Verification_Code.Text))
            {
                this.label8.Text = string.Empty;
                this.button1.Enabled = false;
            }
            else
            {
                this.label8.Text = string.Empty;
                this.button1.Enabled = true;
            }
        }
    }
}